Array.prototype.distinct = function(){
    var arr = this,
        result = [],
        i,
        j,
        len = arr.length;
    for(i = 0; i < len; i++){
        for(j = i + 1; j < len; j++){
            if(arr[i].Store.toString() === arr[j].Store.toString()){
                j = ++i;
            }
        }
        result.push(arr[i]);
    }
    return result;
}

Array.prototype.distinct1 = function(){
    var arr = this,
        result = [],
        i,
        j,
        len = arr.length;
    for(i = 0; i < len; i++){
        for(j = i + 1; j < len; j++){
            if(arr[i].Bm.toString() === arr[j].Bm.toString()){
                j = ++i;
            }
        }
        result.push(arr[i]);
    }
    return result;
}

Array.prototype.distinct2 = function(){
    var arr = this,
        result = [],
        i,
        j,
        len = arr.length;
    for(i = 0; i < len; i++){
        for(j = i + 1; j < len; j++){
            if(arr[i].UserNo.toString() === arr[j].UserNo.toString()){
                j = ++i;
            }
        }
        result.push(arr[i]);
    }
    return result;
}

var pageSize = 99999999;
var vm = new Vue({
    el: "#app",
    data:function(){
        return{
            YueMan:"",
            //限行信息
            NowDay:"",
            TrafficInfo:"",
            //派车弹框Start
            PaiID:'',
            CarSelect:'',
            StoreSelect:'',
            BmSelect:'',
            UserSelect:'',
            PaiModel:false,
            PaiModelItem:{
                Store:'',
                Bm:'',
                UserNo:'',
                Name:'',
                CarNo:'',
                PhoneNo:'',
                PaiIsOrNotPin:''
            },
            //派车弹框End
            //选择司机弹框
            SjModel:false,
            SjName:'',
            //撤销约车弹框Start
            YueCheItem:{
                YueCheID:'',
                YueMan:'',
                YueManNo:'',
                YueCheReason:''
            },
            YueCheModel:false,
            //撤销约车弹框End
            //搜索结果
            columns1: [
                {
                    type: 'selection',
                    align: 'center',
                    width:52,
                },
                {
                    type: 'index',
                    title: '序号',
                    align: 'center',
                },
                {
                    title: '单据编号',
                    key: 'DocNo',
                    align: 'center',
                },
                {
                    title: '调度类型',
                    key: 'Status',
                    align: 'center',
                    render:(h,params)=> {
                        var Status = params.row.Status;
                        if(Status=="1"){
                            Status = '已约车';
                        }
                        if(Status=="2"){
                            Status = '已派车';
                        }
                        if(Status=="3"){
                            Status = '已返回';
                        }
                        return h('span',Status);
                    }
                },
                {
                    title: '约车人',
                    key: 'YueMan',
                    align: 'center',
                },
                {
                    title: '中心店面',
                    key: 'YueStore',
                    align: 'center',
                },
                {
                    title: '约车人部门',
                    key: 'YueBm',
                    align: 'center',
                },
                {
                    title: '目的地',
                    key: 'ArrivePlace',
                    align: 'center',
                },
                {
                    title: '预计出发时间',
                    key: 'YueStartDate',
                    align: 'center',
                },
                {
                    title: '预计返回时间',
                    key: 'YueEndDate',
                    align: 'center',
                },
                {
                    title: '约车事由',
                    key: 'YueReason',
                    align: 'center',
                },
                {
                    title: '同行人数',
                    key: 'YueManNumber',
                    align: 'center',
                },
                {
                    title: '随车物品',
                    key: 'YueGoods',
                    align: 'center',
                },
                {
                    title: '约车特殊情况说明',
                    key: 'YueSpecialExplain',
                    align: 'center',
                },
                {
                    title: '操作',
                    key: 'action',
                    width:150,
                    align: 'center',
                    render: (h, params) => {
                        return h('div', [
                            h('Button', {
                                props: {
                                    type: 'ghost',
                                    size: 'small',
                                    icon:'trash-a',
                                },
                                attrs: {
                                    title: '撤销约车'
                                },
                                style: {
                                    marginRight: '5px',
                                    border : '0',
                                    fontSize: '16px',
                                },
                                on: {
                                    click: () => {
                                        var Status = params.row.Status;
                                        if(Status=="1"){
                                            this.YueCheItem.YueCheID=params.row.ID;
                                            this.YueCheItem.YueMan=params.row.YueMan;
                                            this.YueCheItem.YueManNo=params.row.YueManNo;
                                            this.YueCheModel=true;
                                        }
                                    }
                                }
                            }),
                        ]);
                    }


                }
            ],
            columns2: [
                {
                    type: 'index',
                    title: '序号',
                    width:46,
                    align: 'center',
                },
                {
                    title: '员工编号',
                    key: 'UserNo',
                    width:90,
                    align: 'center',
                },
                {
                    title: '中心店面',
                    key: 'Store',
                    width:75,
                    align: 'center',
                },
                {
                    title: '部门',
                    key: 'Bm',
                    width:75,
                    align: 'center',
                },
                {
                    title: '姓名',
                    key: 'Name',
                    width:75,
                    align: 'center',
                },
                {
                    title: '性别',
                    key: 'Sex',
                    width:100,
                    align: 'center',
                    render:(h,params)=> {
                        var Sex = params.row.Sex;
                        if(Sex=="0"){
                            Sex = '男';
                        }
                        if(Sex=="1"){
                            Sex = '女';
                        }
                        return h('span',Sex);
                    }
                },
                {
                    title: '生日',
                    key: 'Birthday',
                    width:100,
                    align: 'center',
                },
                {
                    title: '手机号',
                    key: 'PhoneNo',
                    width:100,
                    align: 'center',
                },
                {
                    title: '准驾车型',
                    key: 'CanUseCarType',
                    width:100,
                    align: 'center',
                },
                {
                    title: '出车状态',
                    key: 'Status',
                    width:75,
                    align: 'center',
                    render:(h,params)=> {
                        var Status = params.row.Status;
                        if(Status=="0"){
                            Status = '空闲';
                        }
                        if(Status=="1"){
                            Status = '出车中';
                        }
                        return h('span',Status);
                    }
                },
                {
                    title: '初次领证日期',
                    key: 'StartHaveLicenseDate',
                    width:90,
                    align: 'center',
                },
                {
                    title: '驾驶证有效期至',
                    key: 'LicenseUntil',
                    width:100,
                    align: 'center',
                },
            ],
            // 接下来绑定数据，分别对应前面的列的key值来进行数据绑定
            data1:[],
            data2:[],
            pageSize2:5,
            currentPageSize2:1,
            selectYueS:[],
            oldYue:[],
            // 初始化信息总条数
            dataCount:0,
            dataCount2:0,
            stripe:true
        }
    },
    created(){
        this.initThisPage();
        var self = this;
        self.querytable('1','9999999999999');

    },
    methods: {
        rowClassName(row,index){
            if(row.BrFlag==1){
                return 'demo-table-info-row';
            }
        },
        initThisPage(){
            var self = this;
            $.ajax({
                url: "./goPCDD/initDisPage", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                async:false,
                dataType: "json", //返回格式为json
                type: "GET", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.status==0){
                        // top.location.href = "../../login.html";
                        window.location.href="./login.html";
                    }else{
                        var a = JSON.parse(data.data);
                        var carnoarr = a.data;
                        self.CarSelect='';
                        self.CarSelect = carnoarr;
                        var storearr = a.data1;
                        storearr = storearr.distinct();
                        self.StoreSelect='';
                        self.StoreSelect = storearr;

                        var trafficArr = JSON.parse(data.data);
                        self.TrafficInfo = trafficArr.data2;
                        var d = new Date().getDay();
                        self.NowDay = d.toString();
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function() {}
            });
        },
        ok () {
        },
        cancel () {
        },
        handleRowChange(currentRow, oldCurrentRow){
            console.log(currentRow)
            console.log(oldCurrentRow)
        },
        goPaiModel(){
            this.PaiModel = true;
        },
        goSjModel(){
          this.querysjtable('1','999999999999');
          this.SjModel = true;
        },
        sjRowClic(CurrentRow){
            var BrFlag = CurrentRow.BrFlag;
            if(BrFlag==1){
                toastr.warning("该司机有违章信息未处理，处理后再执行派车操作");
                return false;
            }
            this.PaiModelItem.Store = CurrentRow.Store;
            this.PaiModelItem.Bm = CurrentRow.Bm;
            this.PaiModelItem.Name = CurrentRow.Name;
            this.PaiModelItem.UserNo = CurrentRow.UserNo;
            this.PaiModelItem.PhoneNo = CurrentRow.PhoneNo;
            this.SjModel = false;
        },
        changeBm(){
            this.BmSelect = null;
            this.PaiModelItem.Bm = '';
            var Store = this.PaiModelItem.Store;
            if(!Store){
                return null;
            }
            var jsondata = {"Store":Store};
            var data_json = JSON.stringify(jsondata)
            var self = this;
            $.ajax({
                url: "./goPCDD/queryBmByStore", //请求的url地址
                dataType: "json", //返回格式为json
                async:false,
                data: data_json, //参数值
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    if(data.status==1){
                        self.BmSelect = '';
                        var a = JSON.parse(data.data);
                        var bmarr = a.data;
                        bmarr = bmarr.distinct1();
                        self.BmSelect = bmarr;
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function() {}
            });

        },
        changeUser(){
            this.UserSelect = '';
            this.PaiModelItem.UserNo = '';
            this.PaiModelItem.Name = '';
            var Store = this.PaiModelItem.Store;
            var Bm = this.PaiModelItem.Bm;
            if(!Store||!Bm){
                return null;
            }
            var jsondata = {"Store":Store,"Bm":Bm};
            var data_json = JSON.stringify(jsondata)
            var self = this;
            $.ajax({
                url: "./goPCDD/queryUserByStoreAndBm", //请求的url地址
                dataType: "json", //返回格式为json
                async:false,
                data: data_json, //参数值
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    if(data.status==1){
                        self.UserSelect = '';
                        var a = JSON.parse(data.data);
                        var userarr = a.data;
                        userarr = userarr.distinct2();
                        self.UserSelect=userarr
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function() {}
            });

        },
        rowClic(a,index){
            this.$refs.selection.toggleSelect(index);
        },
        //8 移除选中人员
        removeYue(index){
            var yue = this.selectYueS[index];
            this.selectYueS.splice(index,1);
            for (var i in  this.data1){
                if (this.data1[i].DocNo==yue.DocNo){
                    this.rowClic(this.data1,Number(i));
                    break;
                }
            }
        },
        //9 点击框变化时
        SeleChange(selection){
            //取消
            if (this.oldYue.length-selection.length>=0){
                for (var i=0; i<this.oldYue.length; i++){
                    var isAdd = true;
                    for (var j=0; j<selection.length; j++){
                        if (this.oldYue[i].DocNo == selection[j].DocNo){
                            isAdd=false;
                        }
                    }
                    if (isAdd){
                        for (var k=0; k<this.selectYueS.length; k++){
                            if (this.selectYueS[k].DocNo==this.oldYue[i].DocNo){
                                this.selectYueS.splice(k,1);
                                break;
                            }
                        }
                    }
                }
            }
            //增加
            if (this.oldYue.length-selection.length<0){
                for (var i=0; i<selection.length; i++){
                    var isAdd = true;
                    for(var k=0; k<this.selectYueS.length; k++){
                        if (selection[i].DocNo==this.selectYueS[k].DocNo){
                            isAdd=false;
                            break;
                        }
                    }
                    if (isAdd){
                        this.selectYueS.push(selection[i]);
                    }
                }
            }
            if (selection.length==0){
                this.oldYue=[];
            }else{
                this.oldYue=selection;
            }
            //人数
            if (this.selectYueS&&this.selectYueS.length==0){
                this.selectYueS = selection;
            }

            if(selection.length>1){
                $("#IsOrNotP1").attr("style","display:none");
                $("#IsOrNotP2").attr("style","");
            }else if(selection.length==1){
                $("#IsOrNotP1").attr("style","");
                $("#IsOrNotP2").attr("style","display:none");
            }else{
                $("#IsOrNotP1").attr("style","display:none");
                $("#IsOrNotP2").attr("style","display:none");
            }
        },
        getYue(){
            var info ;
            var total;
            var jsondata={"page":this.currentPageSize2, "rows":5};
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goPCDD/queryAllYueDis", //请求的url地址
                dataType: "json", //返回格式为json
                async:false,
                data: data_json, //参数值
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    info = eval(data.data);
                    total = data.totalnum;
                },
                error: function() {},
            });
            if(info){
                this.data1 = info;
                this.dataCount = total;
            }
        },
        querytable(pageNum,pageSize){
            var info ;
            var total;
            var YueMan = this.YueMan;
            var jsondata={"page":pageNum, "row":pageSize,"YueMan":YueMan};
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goPCDD/queryAllDis", //请求的url地址
                dataType: "json", //返回格式为json
                async:false,
                data: data_json, //参数值
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    info = eval(data.data);
                    total = data.totalnum;
                },
                error: function() {},
            });
            if(info){
                this.data1 = info;
                this.dataCount = total;
            }
        },
        //查询所有司机
        querysjtable(pageNum,pageSize){
            var info ;
            var total;
            var Name = this.SjName;
            var jsondata={"page":pageNum, "row":pageSize,"Name":Name};
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goPCDD/queryAllFreeMen", //请求的url地址
                dataType: "json", //返回格式为json
                async:false,
                data: data_json, //参数值
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    info = eval(data.data);
                    total = data.totalnum;
                },
                error: function() {},
            });
            if(info){
                this.data2 = info;
                this.dataCount2 = total;
            }
        },
        // 2 分页
        changepage(index){
            this.querytable(index,pageSize);
        },
        changepage2(index){
            this.currentPageSize2 = index;
            this.getYue();
        },
        //判断是否为面积
        isMianJi(mj){
            var reg = /^[1-9]+\d*(\.\d{0,2})?$|^0?\.\d{0,2}$/;
            if(reg.test(mj)){
                return true;
            }else{
                return false;
            }
        },
        SavePaiInfo(params){
            var selectYueS = this.selectYueS;
            var self = this;
            var EditNameStr = parent.$("#uname1").text();
            var InputMan1 = EditNameStr;
            var SjNo = this.PaiModelItem.UserNo;
            var SjName = this.PaiModelItem.Name;
            var SjPhoneNo = this.PaiModelItem.PhoneNo;
            if(selectYueS.length<=0){
                toastr.warning("请选择约车单据")
                return null;
            }
            if(SjNo==''||SjNo==null){
                toastr.warning("请选择司机姓名")
                return null;
            }
            var PaiCarNo = this.PaiModelItem.CarNo;
            if(PaiCarNo ==''||PaiCarNo ==null){
                toastr.warning("请选择车牌号")
                return null;
            }
            var Pin = 0;
            if(selectYueS.length>1){
                Pin = 1;
            }
            var jsondata = {"selectYueS":selectYueS,"PaiMan":SjNo,"PaiCarNo":PaiCarNo,"SjPhoneNo":SjPhoneNo,"InputMan1":InputMan1,"PaiIsOrNotPin":Pin,"SjName":SjName};
            var data_json = JSON.stringify(jsondata)
            $.ajax({
                url: "./goPCDD/SavePaiInfo", //请求的url地址
                dataType: "json", //返回格式为json
                async:false,
                data: data_json, //参数值
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    if(data.status=="1"){
                        var index ;
                        var tempSelect = self.CarSelect;
                        for(var i=0;i<tempSelect.length;i++){
                            if(tempSelect[i].CarNo.toString()==self.PaiModelItem.CarNo.toString()){
                                index = i;
                            }
                        }
                        tempSelect.splice(index,1);
                        self.CarSelect='';
                        self.CarSelect=tempSelect;
                        self.PaiModelItem.UserNo = '';
                        self.PaiModelItem.Name = '';
                        self.PaiModelItem.CarNo = '';
                        self.PaiModelItem.Store = '';
                        self.PaiModelItem.Bm = '';
                        self.PaiModelItem.PaiIsOrNotPin = '';
                        self.currentPageSize2 = 1;
                        self.selectYueS = [];
                        self.oldYue = [];
                        self.dataCount = 0;
                        self.data1 = [];
                        toastr.success('派车成功');
                    }else{
                        toastr.error('派车失败，请重新派车');
                    }
                },
                error: function() {
                    toastr.error('派车失败，请刷新页面');
                },
            });
            this.querytable("1","99999999999");
        },
        CheYue(){
            var self = this;
            var ID = this.YueCheItem.YueCheID;
            var YueCheReason = this.YueCheItem.YueCheReason;
            var YueManNo = this.YueCheItem.YueManNo;
            var YueMan = this.YueCheItem.YueMan;
            if(YueCheReason==null||YueCheReason==""){
                toastr.error("请录入撤销原因");
                return false;
            }
            if(ID==null||ID==""){
                toastr.error("员工编号未获取，请联系管理员");
                return false;
            }
            var jsondata = {"ID":ID,"YueCheReason":YueCheReason,"YueManNo":YueManNo};
            var data_json = JSON.stringify(jsondata)
            $.ajax({
                url: "./goPCDD/CheYue", //请求的url地址
                dataType: "json", //返回格式为json
                async:false,
                data: data_json, //参数值
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    if(data.status=="1"){
                        toastr.success("撤销约车成功，已发送信息给约车人:"+YueMan);
                        self.querytable('1','9999999999999');
                    }
                },
                error: function() {
                    toastr.error('派车失败，请刷新页面');
                },
            });
        }
    }
})